﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Threading;

namespace QLTV.Forms.KhaiBao
{
    public partial class frmContract : BaseForm
    {
        public frmContract()
        {
            InitializeComponent();
        }
        public int contractID { get; set; }
        public bool isNew { get; set; }
        public frmContract(int _contractID)
        {
            InitializeComponent();
            contractID = _contractID;
        }



        private void frmContract_Load(object sender, EventArgs e)
        {
           

            if (isNew)
            {
                loadData();

                tblContractBindingSource.AddNew();
                barIn.Enabled = false;
                lookupCode.EditValue = dsCrew.tblViewCrew.Rows[0]["nCrewId"];
                lookUpEdit1.EditValue = dsVessel.tblVesselGroup.Rows[0]["nGroupId"];
            
                dContractDateDateEdit.EditValue = DateTime.Now.Date;
            }
            else
            {
                loadData();
                tblContractTableAdapter.FillByID(dsContract.tblContract, contractID);
            }
        }
        private void loadData()
        {
            this.tblVesselGroupTableAdapter.Fill(this.dsVessel.tblVesselGroup);
            this.tblFlagTableAdapter.Fill(this.dsVessel.tblFlag);
            this.tblContractTableAdapter.Fill(this.dsContract.tblContract);
            tblVesselTableAdapter.FillByManaged(dsVessel.tblVessel);
            this.tblRankTableAdapter.Fill(dsRanking.tblRank);
            // TODO: This line of code loads data into the 'dsCrew.tblViewCrew' table. You can move, or remove it, as needed.
            this.tblViewCrewTableAdapter.FillForContract(this.dsCrew.tblViewCrew);
            // TODO: This line of code loads data into the 'dsVessel.tblVesselGroup' table. You can move, or remove it, as needed.
        }


        private void lookUpEdit2_EditValueChanged(object sender, EventArgs e)
        {
            SearchLookUpEdit lookup = sender as SearchLookUpEdit;
            BindingManagerBase bm = BindingContext[lookup.Properties.DataSource];
            bm.Position = lookup.Properties.GetIndexByKeyValue(lookup.EditValue);

            if (lookup.EditValue != null)
            {
                //1= passport
                if (tblImmigrationCrewTableAdapter1.PassportNumber(lookup.EditValue.ToString(), 1) != null)
                    txtpassport.EditValue = tblImmigrationCrewTableAdapter1.PassportNumber(lookup.EditValue.ToString(), 1);
                else
                    txtpassport.EditValue = "NONE";
                //3= seaman book
                if (tblImmigrationCrewTableAdapter1.PassportNumber(lookup.EditValue.ToString(), 3) != null)
                    txtSeamanBook.EditValue = tblImmigrationCrewTableAdapter1.PassportNumber(lookup.EditValue.ToString(), 3);
                else
                    txtSeamanBook.EditValue = "NONE";
            }
        }

        private void nVesselIdTextEdit_EditValueChanged(object sender, EventArgs e)
        {
            SearchLookUpEdit lookup = sender as SearchLookUpEdit;
            BindingManagerBase bm = BindingContext[lookup.Properties.DataSource];
            bm.Position = lookup.Properties.GetIndexByKeyValue(lookup.EditValue);
            
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            validateError.Validate();
            if (validateError.GetInvalidControls().Count == 0)
            {
                tblContractBindingSource.EndEdit();
                tblRankBindingSource.EndEdit();
                tblContractTableAdapter.Update(dsContract.tblContract);
                tblRankTableAdapter.Update(dsRanking.tblRank);
                alertControl1.Show(this, rm.GetString("sMessageCaption", myCultureInfo), rm.GetString("sMessageSuccess", myCultureInfo));
                this.Close();
            }
        }

        private void lookupRank_EditValueChanged(object sender, EventArgs e)
        {
            LookUpEdit lookup = sender as LookUpEdit;
            BindingManagerBase bm = BindingContext[lookup.Properties.DataSource];
            bm.Position = lookup.Properties.GetDataSourceRowIndex(lookup.Properties.ValueMember, lookup.EditValue);
        }
        Forms.frmPleaseWait _wait;
        private void barIn_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Thread lengthyProcessThread = new Thread(XemHD) { IsBackground = true };
            lengthyProcessThread.Start();
            _wait = new Forms.frmPleaseWait();
            check = true;
            _wait.ShowDialog();

        }
        public bool check { get; set; }
        public void XemHD()
        {

            Ultilities.preActive(@"{0}\Reports\contract.doc");
            string contractDate =((DateTime) dContractDateDateEdit.EditValue).ToString("dd-MMM-yy");
            string Ho = Ultilities.RemoveSign4VietnameseString( txtHo.EditValue.ToString());
            string dem =  Ultilities.RemoveSign4VietnameseString(txtDem.EditValue.ToString());
            string ten =  Ultilities.RemoveSign4VietnameseString(txtTen.EditValue.ToString());
            string rank = lookupRank.Properties.GetDisplayText(lookupRank.EditValue);
            string nationality = txtNationality.EditValue.ToString();
            string passport = txtpassport.EditValue.ToString();
            string seamanBook = txtSeamanBook.EditValue.ToString();
            string dateOfBirth = String.IsNullOrEmpty(dateBirth.EditValue.ToString()) ? "" : ((DateTime)(dateBirth.EditValue)).ToString("dd-MMM-yyyy");
            string birthPlace = Ultilities.RemoveSign4VietnameseString(String.IsNullOrEmpty(txtPlace.EditValue.ToString()) ? "" : txtPlace.EditValue.ToString());
            string shipName = nVesselIdTextEdit.Properties.GetDisplayText(nVesselIdTextEdit.EditValue);
            string flag = nVesselIdTextEdit1.Properties.GetDisplayText(nVesselIdTextEdit1.EditValue);
            string port = nVesselIdTextEdit2.Properties.GetDisplayText(nVesselIdTextEdit2.EditValue);
            string period = txtPeriod.EditValue.ToString();
            string basicMonthlyWage = txtBasicMonthlyWages.EditValue.ToString();
            string monthlyLeavePay = txtMonthlyLeavePay.EditValue.ToString();
            string overtime = txtOvertime.EditValue.ToString();
            string phucap = txtPhuCap.EditValue.ToString();
            string imo = txtIMO.EditValue.ToString();

            Ultilities.replaceWord("<%Ho%>", Ho, Ultilities.doc);
            Ultilities.replaceWord("<%Dem%>", dem, Ultilities.doc);
            Ultilities.replaceWord("<%Ten%>", ten, Ultilities.doc);
            Ultilities.replaceWord("<%BirthPlace%>", birthPlace, Ultilities.doc);
            Ultilities.replaceWord("<%Rank%>", rank, Ultilities.doc);
            Ultilities.replaceWord("<%Nationality%>", nationality, Ultilities.doc);
            Ultilities.replaceWord("<%Passport%>", passport, Ultilities.doc);
            Ultilities.replaceWord("<%DateOfBirth%>", dateOfBirth, Ultilities.doc);
            Ultilities.replaceWord("<%SeamanBook%>", seamanBook, Ultilities.doc);
            Ultilities.replaceWord("<%ShipName%>", shipName, Ultilities.doc);
            Ultilities.replaceWord("<%Flag%>", flag, Ultilities.doc);
            Ultilities.replaceWord("<%Port%>", port, Ultilities.doc);
            Ultilities.replaceWord("<%Period%>", period, Ultilities.doc);
            Ultilities.replaceWord("<%BasicWage%>", basicMonthlyWage, Ultilities.doc);
            Ultilities.replaceWord("<%MonthlyLeave%>", monthlyLeavePay, Ultilities.doc);
            Ultilities.replaceWord("<%Overtime%>", overtime, Ultilities.doc);
            Ultilities.replaceWord("<%PhucCap%>", phucap, Ultilities.doc);
            Ultilities.replaceWord("<%Date%>", contractDate, Ultilities.doc);
            Ultilities.replaceWord("<%IMO%>", imo, Ultilities.doc);
            Ultilities.Active();
            if (check)
                _wait.ShouldCloseNow = true;
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void lookUpEdit1_EditValueChanged(object sender, EventArgs e)
        {
            //if (lookUpEdit1.EditValue != DBNull.Value)
            //{
            //    tblVesselBindingSource.Filter = String.Format("nGroupId='{0}'", (int)lookUpEdit1.EditValue);
               

            //    if (lookUpEdit1.Text.ToString().Trim() == "A")
            //    {
            //        txtMonthlyLeavePay.DataBindings.RemoveAt(0);
            //        this.txtMonthlyLeavePay.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.tblRankBindingSource, "Overtime A", true));
            //    }
            //    if (lookUpEdit1.Text.ToString().Trim() == "B")
            //    {
            //        txtMonthlyLeavePay.DataBindings.RemoveAt(0);
            //        this.txtMonthlyLeavePay.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.tblRankBindingSource, "Overtime B", true));
            //    }
            //}
        }

    }
}